import type { conversation, messageFormData, messages } from '@/api/messages/type'
import { v4 } from 'uuid'

//封装保存本地消息或WebSocket消息的方法
export const save = (conversations: conversation[], message: messageFormData) => {
    //发送成功将消息存到本地和仓库中
    for (let i = 0; i < conversations.length; i++) {
        if (message.conversation_id !== conversations[i].conversation_id) {
            continue
        }
        if (message.conversation_id === conversations[i].conversation_id) {
            const pushMessage: messages = {
                ...message,
                message_id: v4(),
            }
            conversations[i].messages.push(pushMessage)
        }
        break
    }
    return conversations
}
